<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,viewport-fit=cover,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
    <title>个人中心</title>
    <script src="js/rem.js"></script>
    <link rel="stylesheet" href="css/weui.min.css">
    <link rel="stylesheet" href="css/jquery-weui.min.css">
    <link rel="stylesheet" href="css/public.css">
    <link rel="stylesheet" href="css/user.css">
    <script>(function() {var _53code = document.createElement("script");_53code.src = "https://tb.53kf.com/code/code/96d8f960b261e49515450f8953b044bc5/1";var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(_53code, s);})();</script>
</head>
<body>
    <div class="pub-container user-consultation">
        <div class="consultation-info space-border bgwhite plr15" id="orderInfo">
            <!-- <div class="user-box box1">
                <p class="consultation-time">订单号<span>38474758599599</span></p>
            </div>
            <div class="user-box box3 plr15">
                <div class="sq-info">
                    <p><span class="txt1">会诊时间:</span><span>2021-02-18</span></p>
                    <p><span class="txt1">会诊专家:</span><span>王小二</span></p>
                    <p><span class="txt1">会诊科室:</span><span>肠胃外科</span></p>
                    <p><span class="txt1">会诊方式:</span><span>门诊会诊</span></p>
                    <p><span class="txt1">会诊费:</span><span class="money">3000</span><span class="unit">元</span></p>
                </div>
            </div> -->
        </div>
        <div class="consultation-info space-border bgwhite plr15">
            <div class="user-box box1">
                <h4 class="user-pub-tit">选择支付方式</h4>
            </div>
            <ul class="pay-types">
                <li data-type="zhifubao" class="pay-type zhifubao">支付宝</li>
                <li data-type="wechat" class="pay-type wechat active">微信</li>
                <!-- <li data-type="bank" class="pay-type bank">在线支付</li> -->
            </ul>
            <input class="pub-submit-btn" type="button" value="立即支付" id="pay">
            <p class="bottom-tip">遇到支付问题请联系客服，客服电话：400-990-0686</p>
            <div class="custom-box">
                <a class="custom-btn" href="https://tb.53kf.com/code/client/96d8f960b261e49515450f8953b044bc5/1">咨询客服</a>
            </div>
        </div>
        <div class="pay-tip2 plr15">
            <p>*注：</p>
            <p>1.您可以在会诊开始3天内之前随时取消会诊进行退款，会诊费会在7天内退回您的支付账户；</p>
            <p>2.会诊开始前，3天内不予退款敬请谅解。</p>
        </div>
    </div>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script src="js/jquery-weui.min.js"></script>
    <script src="js/md5.js"></script>
    <script src="js/public.js"></script>
    <script>
        var code = getQueryString('code');
        var openid;
        var orderNo = getQueryString('orderNo');
        var orderId = getQueryString('orderId');
        var consulationTime = getQueryString('consulationTime');
        var consulationSpecialist = getQueryString('consulationSpecialist');
        var consulationDepartment = getQueryString('consulationDepartment');
        var consulationTime = getQueryString('consulationTime');
        var money = getQueryString('money');
        var time = getQueryString('time');
        if(code){
            // 获取openid
            getOpenid();
        }else {
            $('#pay').click(function(){
                $.showLoading();
                var type = $('.pay-types li.active').attr('data-type');
                if(type === 'wechat'){
                    // 获取code
                    getCode();
                }else if(type === 'zhifubao'){
                    getPayOption('2');
                }
            })
        }
        $('.pay-types li').click(function(){
            $(this).addClass('active').siblings().removeClass('active');
        })
        // $('#pay').click(function(){
        //     var type = $('.pay-types li.active').attr('data-type');
        //     if(type === 'wechat'){

        //     }
        // })
        // 展示用户基本信息
        $('#orderInfo').html('<div class="user-box box1">' + 
            '<p class="consultation-time">订单号<span>' + orderNo + '</span></p>' + 
        '</div>' + 
        '<div class="user-box box3 plr15">' + 
            '<div class="sq-info noborder">' + 
                '<p><span class="txt1">会诊时间:</span><span>' + consulationTime + '</span></p>' + 
                '<p><span class="txt1">会诊专家:</span><span>' + decodeURIComponent(consulationSpecialist) + '</span></p>' + 
                '<p><span class="txt1">会诊科室:</span><span>' + decodeURIComponent(consulationDepartment) + '</span></p>' + 
                '<p><span class="txt1">会诊方式:</span><span>' + decodeURIComponent(getQueryString('type')) + '</span></p>' + 
                '<p class="w100"><span class="txt1">会诊费:</span><span class="money">' + decodeURIComponent(money) + '</span><span class="unit">元</span></p>' + 
            '</div>' + 
        '</div>')
        // 支付成功后修改订单状态 - 暂未使用
        function updateOrder(orderType, orderStatus){
            http('/pay/updateOrder', {
                hasToken: false,
                data: {
                    orderId: orderId,
                    orderStatus: orderStatus,
                    orderType: orderType
                },
                success: function(res){
                    console.log('orderStatus', orderStatus)
                    if(orderStatus == '04'){
                        window.location.replace('pay-success.html?orderNo=' + orderNo);
                    }
                }
            })
        }
        // 微信支付
        function getCode(){
            if (code === '' || code == null) {
                var thisUrl = window.location.href;
                thisUrl = encodeURIComponent(thisUrl);
                var theWx = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appId + '&redirect_uri=';
                var url = theWx + thisUrl + '&response_type=code&scope=snsapi_base&state=123#wechat_redirect';
                window.location.href = url;
            }
        }
        function getOpenid(){
            var aJson = {
                'jsCode': code,
            };
            $.ajax({
                url: host+'/user/goVxLogin',
                method: 'POST',
                dataType: 'json',
                contentType:'application/json',
                data: JSON.stringify(aJson),
                success: function(res) {
                    console.log(res);
                    if (res.code == '01') {
                        openid = res.data.openid;
                        getPayOption('1');
                    }else{
                        toast(res.message);
                    }
                }
            });
        }

        function getPayOption(orderType){
            var option = {
                orderNo: orderNo,
                orderType: orderType,
                openid: openid
            }
            http('/pay/goPay', {
                hasToken: false,
                data: option,
                success: function(res){
                    if(res.code === '01'){
                        if(orderType == '1'){
                            wecahtPay(orderType, res.data);
                        } else if(orderType == '2'){
                            console.log(res.data)
                            $(document.body).append(res.data)
                        }
                    }
                },
                fail: function(){
                    $.hideLoading();
                }
            })
        }

        function wecahtPay(orderType, data){
            var o = {
                "appId": data.appId, //公众号ID，由商户传入     
                "timeStamp": data.timeStamp, //时间戳，自1970年以来的秒数     
                "nonceStr": data.nonceStr, //随机串     
                "package": data.package,     
                "signType": data.signType, //微信签名方式：     
                "paySign": data.paySign //微信签名 
            }
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', o,
                function(res){
                    $.hideLoading();
                    if(res.err_msg == "get_brand_wcpay_request:ok" ){
                    // 使用以上方式判断前端返回,微信团队郑重提示：
                        //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                        // updateOrder(orderType, '04')
                        window.location.replace('pay-success.html?orderNo=' + orderNo);
                    } 
                    // else {
                        // updateOrder(orderType, '05')
                    // }
                }
            ); 
        }
        
    </script>
</body>
</html>